{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cd29f7c3",
   "metadata": {},
   "source": [
    "## 函数\n",
    "### 函数的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc450f40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 \n"
     ]
    }
   ],
   "source": [
    "# def:定义函数的关键字\n",
    "# fib：函数名\n",
    "# (n)：定义函数的参数\n",
    "\n",
    "\n",
    "def fib(n):    # write Fibonacci series up to n\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    \n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a, b = b, a+b\n",
    "    print()\n",
    "    \n",
    "fib(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "614997a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "鸡灿无敌\n"
     ]
    }
   ],
   "source": [
    "# 尝试写一个 鸡灿无敌 的函数\n",
    "\n",
    "\n",
    "def hello():  # 定义函数\n",
    "    \"\"\"print 鸡灿无敌\"\"\"\n",
    "    \n",
    "    print(\"鸡灿无敌\")\n",
    "    \n",
    "hello()  # 调用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0e9dd775",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'a'}\n"
     ]
    }
   ],
   "source": [
    "def a(word):\n",
    "    \"\"\"print vowels\"\"\"\n",
    "    \n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    print(found)\n",
    "\n",
    "a(\"abcde\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0883be2",
   "metadata": {},
   "source": [
    "### 函数的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e4b7a00a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, Zuqiying!\n"
     ]
    }
   ],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"print hello username!\"\"\"\n",
    "    \n",
    "    print(\"hello, \" + username.title() + \"!\")\n",
    "greet_user(\"ZUQIYING\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "47a311d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have a cxc.\n",
      "cxc's type is a chicken.\n"
     ]
    }
   ],
   "source": [
    "# 位置参数\n",
    "\n",
    "def describe_pet(pet_type, name):\n",
    "    \"\"\"print pet information\"\"\"\n",
    "    \n",
    "    print(\"I have a %s.\" % name)\n",
    "    print(\"%s's type is a %s.\" % (name, pet_type))\n",
    "\n",
    "describe_pet(\"chicken\",\"cxc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ef120193",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have a cxc.\n",
      "cxc's type is a chicken.\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数\n",
    "\n",
    "def describe_pet(pet_type, name):\n",
    "    \"\"\"print pet information\"\"\"\n",
    "    \n",
    "    print(\"I have a %s.\" % name)\n",
    "    print(\"%s's type is a %s.\" % (name, pet_type))\n",
    "\n",
    "describe_pet(name = \"cxc\", pet_type = \"chicken\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5530bc8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have a cxc.\n",
      "cxc's type is a chicken.\n",
      "I have a qq.\n",
      "qq's type is a cat.\n"
     ]
    }
   ],
   "source": [
    "# 默认参数(写在形式参数中)\n",
    "\n",
    "def describe_pet(name, pet_type = \"chicken\"):\n",
    "    \"\"\"print pet information, pet_type == 'chicken'\"\"\"\n",
    "    \n",
    "    print(\"I have a %s.\" % name)\n",
    "    print(\"%s's type is a %s.\" % (name, pet_type))\n",
    "\n",
    "describe_pet(\"cxc\")\n",
    "describe_pet(\"qq\",\"cat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d684a13",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
